#!/usr/bin/env bash

set -e

if [ -z "$DEPENDABOT_NATIVE_HELPERS_PATH" ]; then
  echo "Unable to build, DEPENDABOT_NATIVE_HELPERS_PATH is not set"
  exit 1
fi

install_dir="$DEPENDABOT_NATIVE_HELPERS_PATH/go_modules"

if ! [[ "$install_dir" =~ ^/ ]]; then
  echo "$install_dir must be an absolute path"
  exit 1
fi

if [ ! -d "$install_dir/bin" ]; then
  mkdir -p "$install_dir/bin"
fi

helpers_dir="$(dirname "${BASH_SOURCE[0]}")"
cd "$helpers_dir"

os="$(uname -s | tr '[:upper:]' '[:lower:]')"
echo "building $install_dir/bin/helper"

GOOS="$os" GOARCH=amd64 go build -o "$install_dir/bin/helper" .
go clean -cache -modcache
